package cn.springbook.common.starter.swagger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * 说明：Swagger 接口API生成
 * 作者：FH Admin
 */
@Configuration
@EnableOpenApi
@EnableConfigurationProperties(SwaggerAutoConfigProperties.class)
public class SwaggerAutoConfigure {

	@Autowired
	private SwaggerAutoConfigProperties swaggerAutoConfigProperties;

	@Bean
	@ConditionalOnMissingBean // 只有在spring容器中没有这个bean的时候才会注册,如果有则不注册
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerAutoConfigProperties.getScanPackage()))	// 为当前包路径
                .paths(PathSelectors.any())
                .build();
    }

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title(swaggerAutoConfigProperties.getTitle()) 				// 页面标题
				.version(swaggerAutoConfigProperties.getVersion())			// 版本号
				.description(swaggerAutoConfigProperties.getDescription())	// 描述
				.build();
	}

}